package com.whale.cloud.model;

import com.alibaba.fastjson.JSONObject;
import com.whale.cloud.util.StringUtils;

import java.util.List;

/** Created by lide on 2017/6/19. */
public class RequestChecker {
  public static DataResponse requestChecker(List<String> list, DataRequest datarequest) {
    DataResponse dataResponse =
        new DataResponse().setId(datarequest.getId()).setData(new JSONObject());
    if (list.size() > 0) {
      for (int i = 0; i < list.size(); i++) {
        String field = list.get(i);
        if (!datarequest.getData().containsKey(field)) {
          return dataResponse.setMsg(field + " 字段为空").setStatus(Status.FAILED);
        }
        if (!StringUtils.isNotNull(datarequest.getData().get(field))) {
          return dataResponse.setMsg(field + " 为空").setStatus(Status.FAILED);
        }
      }
    }
    return dataResponse.setStatus(Status.SUCCESS);
  }

  public static DataResponse requestCheckerKey(List<String> list, DataRequest datarequest) {
    DataResponse dataResponse =
        new DataResponse().setId(datarequest.getId()).setData(new JSONObject());
    if (list.size() > 0) {
      for (int i = 0; i < list.size(); i++) {
        String field = list.get(i);
        if (!datarequest.getData().containsKey(field)) {
          return dataResponse.setMsg(field + "字段为空").setStatus(Status.FAILED);
        }
      }
    }
    return dataResponse.setStatus(Status.SUCCESS);
  }
}
